package com.zerdoor.controller;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.zerdoor.model.Tree;
import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.RbacRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2015/2/2.
 * 管理员角色管理
 */
@Controller
@RequestMapping("rbacRole")
public class RbacRoleController {
    @Autowired
    private RbacRoleService rbacRoleService;
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(){
        return "/rbac/roles";
    }

    @RequestMapping(value = "/view",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> view(HttpServletRequest request,int page,int rows){
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        PageHelper.startPage(page, rows);
        params.put("title", request.getParameter("title"));
        List<Map<String, Object>> list = rbacRoleService.getRbacRole(params);
        PageInfo pageInfo = new PageInfo(list);
        result.put("rows",list);
        result.put("total",pageInfo.getTotal());
        return result;
    }

    /**
     * 添加角色
     * @param request
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST, produces = "text/htm;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> add(HttpServletRequest request){
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("roleId",null);
        params.put("roleName",request.getParameter("roleName"));
        params.put("description",request.getParameter("description"));
        params.put("isValid", Integer.parseInt(request.getParameter("isValid")));
        try {
            rbacRoleService.addRbacRole(params);
        } catch (Exception e) {
            result.put("errormsg","添加失败");
        }
        return result;
    }

    /**
     * 修改角色
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST, produces = "text/htm;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> edit(HttpServletRequest request,int id){
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("roleId",Integer.parseInt(request.getParameter("id")));
        params.put("roleName",request.getParameter("roleName"));
        params.put("description",request.getParameter("description"));
        params.put("isValid", Integer.parseInt(request.getParameter("isValid")));
        try {
            rbacRoleService.updateRbacRole(params);
        } catch (Exception e) {
            result.put("errormsg", "修改失败");
        }
        return result;
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delete(int id){
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            rbacRoleService.deleteRbacRole(id);
        } catch (Exception e) {
            result.put("errormsg", "删除失败");
        }
        return result;
    }

    @RequestMapping(value = "/activate",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> activate(int id){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("roleId",id);
        try {
            rbacRoleService.updateActivationRbacRole(result);
        } catch (Exception e) {
            result.put("errormsg","激活失败");
        }
        return result;
    }

    @RequestMapping(value = "/stop",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> stop(int id){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("roleId",id);
        try {
            rbacRoleService.updateStopRbacRole(result);
        } catch (Exception e) {
            result.put("errormsg","停用失败");
        }
        return result;
    }

    /**
     * 查询资源树
     * @return
     */
    @RequestMapping(value = "/getResourcesTree",method = RequestMethod.POST)
    @ResponseBody
    public List<Tree> getResourcesTree(){
        List<Tree> treelist = new ArrayList<Tree>();
        List<Map<String,Object>> list = rbacRoleService.getResources();
        for(Map<String,Object> temp1 : list){
            //父类
            if(Integer.parseInt(temp1.get("parent").toString()) == 0){
                Tree t = new Tree();
                t.setId(Integer.parseInt(temp1.get("id").toString()));
                t.setText(temp1.get("name").toString());
                Map<String,String> attr = new HashMap<String, String>();
                attr.put("opIds","");
                t.setAttributes(attr);
                List<Tree> children = new ArrayList<Tree>();
                //子类
                for(Map<String,Object> temp2 : list){
                    if(Integer.parseInt(temp1.get("id").toString()) == Integer.parseInt(temp2.get("parent").toString())){
                        Tree t2 = new Tree();
                        t2.setId(Integer.parseInt(temp2.get("id").toString()));
                        t2.setText(temp2.get("name").toString());
                        Map<String,String> attr2 = new HashMap<String, String>();
                        attr2.put("opIds","");
                        t2.setAttributes(attr2);
                        children.add(t2);
                    }
                }
                t.setChildren(children);
                treelist.add(t);
            }
        }
        return treelist;
    }
    /**
     * 查询操作码树
     * @return
     */
    @RequestMapping(value = "/getOperatingTree",method = RequestMethod.POST)
    @ResponseBody
    public List<Tree> getOperatingTree(){
        List<Tree> treelist = new ArrayList<Tree>();
        List<Map<String,Object>> list = rbacRoleService.getOperatings();
        for(Map<String,Object> temp1 : list){
            //父类
                Tree t = new Tree();
                t.setId(Integer.parseInt(temp1.get("id").toString()));
                t.setText(temp1.get("name").toString());
                treelist.add(t);
        }
        return treelist;
    }

    /**
     * 给角色付资源权限
     * @param roleId 角色编号
     * @param resources 资源对象
     * @return
     */
    @RequestMapping(value = "/saveRro",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveRro(int roleId,String resources){
        Map<String, Object> result = new HashMap<String, Object>();
        Gson gson = new Gson();
        JsonArray arr = gson.fromJson(resources, JsonArray.class);
        try {
            rbacRoleService.updateRro(roleId, arr);
        } catch (Exception e) {
            result.put("errormsg","更改权限失败!");
        }
        return result;
    }
    @RequestMapping(value = "/getOpIdsByRoleId",method = RequestMethod.POST)
    @ResponseBody
    public List<Map<String,Object>> getOpIdsByRoleId(int roleId){
        return rbacRoleService.getOpIdsByRoleId(roleId);
    }
}
